import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * User: 12629
 * Date: 2021/10/21
 * Time: 9:08
 * Description:
 */
public class MyArrayList {

    public int[] elem;//null
    public int usedSize;

    public MyArrayList() {
        this.elem = new int[5];
    }

    // 打印顺序表
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elem[i]+" ");
        }
        System.out.println();
    }

    // 在 pos 位置新增元素
    public void add(int pos, int data) {
        if(pos < 0 || pos > this.usedSize) {
            System.out.println("pos位置不合法！");
            return;
        }
        //扩容函数
        if(this.isFull()) {
            System.out.println("扩容！");
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        //挪开元素
        for (int i = this.usedSize-1; i >= pos ; i--) {
            this.elem[i+1] = this.elem[i];
        }
        this.elem[pos] = data;
        this.usedSize++;
    }

    public boolean isFull() {
        if(this.usedSize == this.elem.length) {
            return true;
        }
        return false;
    }

    public boolean isEmpty() {
        return this.usedSize == 0;
    }
    // 判定是否包含某个元素
    public boolean contains(int toFind) {
        if(isEmpty()) {
            System.out.println("顺序表为空！");
            return false;
        }
        for (int i = 0; i < this.usedSize; i++) {
            if(this.elem[i] == toFind) {
                return true;
            }
        }
        return false;
    }
    // 查找某个元素对应的位置[下标]  找不到返回-1
    public int search(int toFind) {
        if(isEmpty()) {
            System.out.println("顺序表为空！");
            return -1;
        }
        for (int i = 0; i < this.usedSize; i++) {
            if(this.elem[i] == toFind) {
                return i;
            }
        }
        return -1;
    }
    // 获取 pos 位置的元素   pos!=usedSize
    public int getPos(int pos) {
        /*if(pos < 0 || pos >= this.usedSize) {
            System.out.println("pos位置不合法！");
            return -1;
        }else {
            return this.elem[pos];
        }*/
        if(pos < 0 || pos >= this.usedSize) {
            throw new ArrayIndexOutOfBoundsException("pos位置不合法");//自定义的异常
        }
        return this.elem[pos];
    }
    // 获取顺序表有效数据的长度
    public int size() {
        return this.usedSize;
    }

    // 给 pos 位置的元素设为 value  更新
    public void setPos(int pos, int value) {
        if(pos < 0 || pos >= this.usedSize) {
            throw new ArrayIndexOutOfBoundsException("pos位置不合法");//自定义的异常
        }
        this.elem[pos] = value;
    }

    //删除第一次出现的关键字key
    public void remove(int toRemove) {
        //1、判断是否为空
        //2、找到你要删除的元素的下标
        int index = search(toRemove);
        if(index == -1) {
            System.out.println("没有你要删除的数字："+toRemove);
            return;
        }
        for (int i = index; i < this.usedSize-1; i++) {
            this.elem[i] = this.elem[i+1];
        }
        this.usedSize--;//有效减一
        System.out.println("删除成功了！");
    }

    // 清空顺序表
    public void clear() {
        //this.usedSize = 0;
        for (int i = 0; i < this.usedSize; i++) {
            this.elem[i] = 0;//this.elem[i] = null
        }
        this.usedSize = 0;
    }

}
